﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using SOEN344_Term_Assignment.Models;

namespace SOEN344_Term_Assignment
{
    public partial class _Default : System.Web.UI.Page
    {
        EventSchedulerDataContext _proxy;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (_proxy == null)
            {
                _proxy = new EventSchedulerDataContext();
            }

            

            //List<Event> events = new List<Event>();

            //for (int i = 0; i < 10; i++)
            //{
            //    Event dummyevent = new Event();
            //    dummyevent.Title = "Event " + i;
            //    dummyevent.Capacity = 100 * i;
            //    dummyevent.Date = DateTime.Now.AddMonths(i%12).AddDays(i);
            //    //dummyevent.Type = 1;
            //    //dummyevent.Type.Name = "working on " + i + " project(s)";
            //    dummyevent.isOpen = (i % 2 != 0);

            //    events.Add(dummyevent);
            //}

            //foreach (Event dummyevent in events)
            //{
            //    _proxy.Events.Add(dummyevent);
            //    _proxy.SubmitChanges();
            //}

            if (!IsPostBack)
            {

                //List<Event> eventsFromDB = _proxy.Events.ToList();

                EventGridView.DataSource = _proxy.Events;
                EventGridView.DataBind();

                if (EventGridView.Rows.Count > 1)
                {
                    EventGridView.Visible = true;
                }
                else
                {
                    EventGridView.Visible = false;
                }                

            }
            
        }

        protected void EventGridView_DataBound(object sender, EventArgs e)
        {
            if (EventGridView.Columns.Count > 1)
            {
                EventGridView.Columns[1].Visible = false;
            }
        }

        protected void SubmitButton_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(ClientLastNameTextBox.Text))
            {

                if (_proxy == null)
                {
                    _proxy = new EventSchedulerDataContext();
                }

                foreach (GridViewRow row in EventGridView.Rows)
                {
                    DropDownList seatDDL = row.FindControl("numberOfSeatsColumn") as DropDownList;
                    
                    int numSeats = 0;

                    if (seatDDL != null)
                    {
                        numSeats = Convert.ToInt32((seatDDL).SelectedItem.Text);
                    }

                    int failCount = 0;

                    if (numSeats != 0)
                    {
                        for (int i = 0; i < numSeats; i++)
                        {
                            int eventID = Convert.ToInt32(row.Cells[1].Text);
                            var currentEvent = _proxy.Events.Where(x => x.ID == eventID).FirstOrDefault();                            

                            if (_proxy.Bookings.Where(x => x.EventID == eventID).Count() < currentEvent.Capacity
                                && currentEvent.isOpen)
                            {

                                Booking b = new Booking();
                                b.FirstName = ClientFirstNameTextBox.Text;
                                b.LastName = ClientLastNameTextBox.Text;
                                b.EventID = eventID;
                                b.Event = currentEvent;
                                b.DateTime = DateTime.Now;

                                _proxy.Bookings.InsertOnSubmit(b);
                                _proxy.SubmitChanges();

                                if (_proxy.Bookings.Where(x => x.EventID == eventID).Count() == currentEvent.Capacity
                                    && currentEvent.isOpen)
                                {
                                    currentEvent.isOpen = false;
                                    _proxy.SubmitChanges();
                                }
                                
                            }
                            else
                            {
                                failCount++;
                            }
                        }
                    }
                    if (failCount > 0)
                    {
                        Response.Write("<script>alert('" + row.Cells[2].Text + "is over capacity. Only able to book " + (numSeats - failCount) + " seats." +"');</script>");
                    }

                }
            }
            else
            {
                //display warning that lastname cannot be empty
            }
        }
    }
}
